//@charset "utf-8";
/**
 * 取客户端数据会用到的一些时间处理函数
 */
 define(function (require, exports, module) {
	var Period = {};
	Period.REAL_TIME = 0;
	Period.CLOSING_DETAILS = 4096;
	Period.MINUTE_TENDENCY = 8192;
	Period.ONE_MINUTE = 12289;
	Period.FIVE_MINUTES = 12293;
	Period.FIFTEEN_MINUTES = 12303;
	Period.THIRTY_MINUTES = 12318;
	Period.ONE_HOUR = 12348;
	Period.DAY = 16384;
	Period.WEEK = 20480;
	Period.MONTH = 24576;
	Period.YEAR = 28672;

	Period.getName = function (period) {
		period = parseInt(period);
		switch (period) {
			case Period.REAL_TIME :
				return '实时';
			case Period.CLOSING_DETAILS :
				return '成交明细';	
			case Period.MINUTE_TENDENCY :
				return '分钟走势';
			case Period.ONE_MINUTE :
				return '1分钟线';
			case Period.FIVE_MINUTES :
				return '5分钟线';
			case Period.FIFTEEN_MINUTES :
				return '15分钟线';
			case Period.THIRTY_MINUTES :
				return '30分钟线';
			case Period.ONE_HOUR :
				return '60分钟线';
			case Period.DAY :
				return '日线';
			case Period.WEEK :
				return '周线';
			case Period.MONTH :
				return '月线';
			case Period.YEAR :
				return '年线';
		}
	};
	var Time = function () {};

	/**
	 * 格式化"datetime"参数
	 * 分钟周期的时间格式，因为长度范围的原因，采用了特殊的位运算
	 * 
	 * @param 	Integer|String	time	20121010|201210101130|2012-11-20 11:30
	 * @param 	Integer|String	period	16384|12348
	 * @return	String	20121010|118117086
	 */
	Time.prototype.formatDateTime = function (time, period) {
		period = period || Period.DAY;
		var timeStr = String(time);
		var formatedTime = false;
		if (Period.REAL_TIME <= period && Period.CLOSING_DETAILS >= period) {
			// TO DO
		} else if (Period.MINUTE_TENDENCY <= period && Period.ONE_HOUR >= period) {
			var minute, hour, day, month, year;
			timeStr = timeStr.replace(/(-|:|\s)/g, '');
			year = parseInt(timeStr.substr(0, 4), 10);
			month = parseInt(timeStr.substr(4, 2), 10);
			day = parseInt(timeStr.substr(6, 2), 10);
			hour = parseInt(timeStr.substr(8, 2), 10);
			minute = parseInt(timeStr.substr(10, 2), 10);
			year = year - 1900;
			formatedTime = (year << 20) + (month << 16) + (day << 11)  + (hour << 6) + (minute << 0);
		} else if (Period.DAY <= period) {
			return timeStr;
		}
		
		return formatedTime;
	};

	/**
	 * 解析"datetime"参数
	 * @param 	Integer|String	time	20121010|118117086
	 * @param 	Integer|String	period	16384|12348
	 * @return	String	2012-10-10|2012-10-10 11:30
	 */
	Time.prototype.parseDateTime = function (time, period, format) {
		// TO DO format操作
		period = period || Period.DAY;
		var easilyReadTime = '';
		if (Period.REAL_TIME <= period && Period.CLOSING_DETAILS >= period) {
			// TO DO
		} else if (Period.MINUTE_TENDENCY <= period && Period.ONE_HOUR >= period) {
			var year, month, day, hour, minute;
			year = time >> 20;
			time = time - (year << 20);
			month = time >> 16;
			time = time - (month << 16);
			day = time >> 11;
			time = time - (day << 11);
			hour = time >> 6;
			time = time - (hour << 6);
			minute = time >> 0;
			year += 1900;
			easilyReadTime = year + '-' + this.repair(month) + '-' + this.repair(day) + ' ' + this.repair(hour) + ':' + this.repair(minute);
		} else if (Period.DAY <= period) {
			var timeStr, day, month, year;
			timeStr = String(time);
			day = timeStr.substring(6,8);
			month = timeStr.substring(4,6);
			year = timeStr.substring(0,4);
			easilyReadTime = year + '-' + month + '-' + day;
		}
		
		return easilyReadTime;
	};

	/**
	 * 生成上一个小时时间
	 * @param String dateTime 格式为YYYYmmddHHii的时间
	 */
	Time.prototype.getPreHour = function (dateTime) {
		// 避免number类型变量调用substring方法
		dateTime += '';
		var year = dateTime.substring(0, 4);
		var month = dateTime.substring(4, 6);
		var day = dateTime.substring(6, 8);
		var hour = dateTime.substring(8, 10);
		if (hour <= 10) {
			day --;
			hour = 15;
		} else if (hour > 15) {
			hour = 15;
		} else if (hour <= 14 && hour > 11) {
			hour = 11;
		} else {
			hour --;
		}
		if (hour >= 10 && hour <= 11) {
			minute = 30;
		} else {
			minute = 0;
		}
		month = month - 1;
		dateTime = new Date(year, month, day, hour, minute);
		var strYear = dateTime.getFullYear();   
		var strDay = dateTime.getDate();   
		var strMonth = dateTime.getMonth() + 1; 
		var strHour = dateTime.getHours();
		var strMinute = dateTime.getMinutes();  
		if(strMonth < 10) {   
			strMonth = "0" + strMonth;   
		} 
		if(strDay < 10) {   
			strDay = "0" + strDay;   
		}  
		if(strHour < 10) {   
			strHour = "0" + strHour;   
		} 
		if(strMinute < 10) {   
			strMinute = "0" + strMinute;   
		}    
		var strYesterday = strYear + '' + strMonth + '' + strDay + '' + strHour + '' + strMinute;  
		return strYesterday;
	};


	/**
	 * 获取前一天的日期
	 * @param String date 日期格式为yyyymmdd
	 */
	Time.prototype.getPreDay = function (date) {
		date += '';
		var year = date.substring(0, 4);
		var month = date.substring(4, 6);
		var day = date.substring(6, 8);
		month = month - 1;
		var dateTime = new Date(year, month, day);
		var preDateTime = dateTime.getTime() - 1000 * 60 * 60 * 24;
		var preDate = new Date();
		preDate.setTime(preDateTime);    
		var strYear = preDate.getFullYear();   
		var strDay = preDate.getDate();   
		var strMonth = preDate.getMonth()+1;   
		if(strMonth < 10) {   
			strMonth = "0" + strMonth;   
		} 
		if(strDay < 10) {   
			strDay = "0" + strDay;   
		}   
		var strYesterday = strYear + '' + strMonth + '' + strDay;  
		return strYesterday;
	};

	/**
	 * 获取上一个周期的日期
	 * @param String date 日期格式为yyyymmddHHii
	 */
	Time.prototype.getPreTime = function (time, period) {
		period = period || Period.DAY;
		if (Period.REAL_TIME <= period && Period.CLOSING_DETAILS >= period) {
			// TO DO
		} else if (Period.MINUTE_TENDENCY <= period && Period.ONE_HOUR >= period) {
			return this.getPreHour(time);
		} else if (Period.DAY <= period) {
			return this.getPreDay(time);
		}
	};

	Time.prototype.repair = function (val) {
		if (val < 10) {
			val = '0' + val;
		}
		return val;
	};

	/**
	 * 反转日期
	 * @param 	Integer|String	time	20121010|118117086
	 * @param 	Integer|String	period	Period.DAY|Period.ONE_HOUR
	 * @return	String	2012-10-10|2012-10-10 11:30
	 */
	Time.prototype.simplify = function (time, period) {
		period = period || Period.DAY;
		var easilyReadTime = '';
		if (Period.REAL_TIME <= period && Period.CLOSING_DETAILS >= period) {
			// TO DO
		} else if (Period.MINUTE_TENDENCY <= period && Period.ONE_HOUR >= period) {
			var year, month, day, hour, minute;
			year = time >> 20;
			time = time - (year << 20);
			month = time >> 16;
			time = time - (month << 16);
			day = time >> 11;
			time = time - (day << 11);
			hour = time >> 6;
			time = time - (hour << 6);
			minute = time >> 0;
			year += 1900;
			easilyReadTime = year + '-' + this.repair(month) + '-' + this.repair(day) + ' ' + this.repair(hour) + ':' + this.repair(minute);
		} else if (Period.DAY <= period) {
			var timeStr, day, month, year;
			timeStr = String(time);
			day = timeStr.substring(6,8);
			month = timeStr.substring(4,6);
			year = timeStr.substring(0,4);
			easilyReadTime = year + '-' + month + '-' + day;
		}
		
		return easilyReadTime;
	};

	module.exports = Time;
});